<?php
// +----------------------------------------------------------------------
// | TP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://www.twothink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: CodeShuHao
// +-----

namespace app\common\controller;


use think\Controller;
use think\facade\Request;

abstract class BasicController extends Controller
{
    public $globals = null;

    public function __construct()
    {
        parent::__construct();

        $this->globals = [
            'module'     => $this->getClassDocCommentByParam('@module', 'unknown @module comments'),
            'controller' => $this->getClassDocCommentByParam('@controller', 'unknown @controller comments'),
            'action'     => $this->getMethodDocCommentByParam('@action', 'unknown @action comments'),
            'desc'       => $this->getMethodDocCommentByParam('@desc'),
        ];

        $this->assign('globals', $this->globals);
    }

    /**
     * 判断请求类型并自动调用
     */
    public function isPost($method = 'Post')
    {
        if ( Request::isPost() )
        {
            //请求方法
            $action = Request::action();
            //后缀支持
//            $suffix = config('controller_suffix');
            //目标方法
            $targetAction = sprintf('%s%s',$action,$method);
            //执行对象
            $instance = [$this,$targetAction];

            // 判定是否存在实例方法
            if( call_user_func_array('method_exists', $instance) === true )
            {
                // 调用并返回
                exit(call_user_func($instance));
            }
        }
    }

    /**
     * 操作错误的跳转方法
     * @param string $message 返回信息
     * @param string $url   跳转地址
     */
    public function failure($message = '', $url = '')
    {
        return $this->ajaxReturn($message, $url, false);
    }

    /**
     * 操作正确的跳转方法
     * @param string $message 返回信息
     * @param string $url   跳转地址
     */
    public function render($message = '', $url = '')
    {
        return $this->ajaxReturn($message, $url, true);
    }

    /**
     * 返回json数据
     * @param string $message 返回信息
     * @param string $url     跳转地址
     * @param bool $status    状态
     */
    public function ajaxReturn($message = '', $url = '', $status = false)
    {
        $data = [
            'info' => $message,
            'status' => $status,
            'url' => $url,
            'ajax' => false,
            'wait' => 3,
        ];

        // 返回JSON数据格式到客户端 包含状态信息
        header('Content-Type:application/json; charset=utf-8');
        exit(json_encode($data,JSON_UNESCAPED_UNICODE));
    }

    /**
     * 获取类注释参数
     * @param unknown $parameter
     * @param string $default
     * @return string|Ambigous <string, boolean>
     */
    protected function getClassDocCommentByParam($parameter,$default = null)
    {
        $class = new \ReflectionClass($this);

        $docComment = $class->getDocComment();

        if( $docComment === false )
        {
            return $default;
        }

        $result = $this->docCommentParse($docComment, $parameter);

        if( $result === false )
        {
            $result = $default;
        }

        return $result;
    }

    /**
     * 获取类方法注释参数
     * @param unknown $parameter
     * @param string $default
     * @param string $method
     * @return string|Ambigous <string, boolean>
     */
    protected function getMethodDocCommentByParam($parameter,$default = null,$method = true)
    {
        $class = new \ReflectionClass($this);

        if( $method === true )
        {
            $method = sprintf('%s%s',\request()->action(),config('action_suffix'));
        }

        if( method_exists($this,$method) === false )
        {
            return $default;
        }

        $method = $class->getMethod($method);

        $docComment = $method->getDocComment();

        if( $docComment === false )
        {
            return $default;
        }

        $result = $this->docCommentParse($docComment, $parameter);

        if( $result === false )
        {
            $result = $default;
        }

        return $result;
    }

    /**
     * 解析注释
     * @param unknown $docComment
     * @param unknown $parameter
     * @return string|boolean
     */
    protected function docCommentParse($docComment,$parameter)
    {
        if( empty($docComment) === false )
        {
            $docblock = explode(PHP_EOL, $docComment);

            foreach ($docblock as $key => $item)
            {
                $offset = strpos($item, $parameter);

                if( $offset )
                {
                    return trim(substr($item, $offset + strlen($parameter)));
                }
            }
        }

        return false;
    }
}